package com.gmu.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gmu.domain.ResResult;
import com.gmu.mapper.TblOrderMapper;
import com.gmu.mapper.TblSampleMapper;
import com.gmu.mapper.TblUserMapper;
import com.gmu.mapper.TblVolunteerMapper;
import com.gmu.pojo.TblOrder;
import com.gmu.pojo.TblSample;
import com.gmu.pojo.TblUser;
import com.gmu.pojo.TblVolunteer;
import com.gmu.service.TblVolunteerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 志愿者申请 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-07-24
 */
@Api(tags = "志愿者管理")
@RestController
@RequestMapping("/gmu/tbl-volunteer")
public class TblVolunteerController {

    @Autowired
    private TblVolunteerService tblVolunteerService;

    @Autowired
    private TblVolunteerMapper tblVolunteerMapper;

    @ApiOperation(value = "志愿者工作申请")
    @PostMapping("/volunteerOrder/{userId}/{activitiesAddressId}")
    public ResResult<?> volunteerOrder(@PathVariable Integer userId,@PathVariable Integer activitiesAddressId){
        Integer count = tblVolunteerMapper.selectCountVolunteer(userId, activitiesAddressId);
        if (count > 0){
            return new ResResult<>(201,"不可重复申请");
        }
        return tblVolunteerService.volunteerOrder(userId,activitiesAddressId);
    }

    @ApiOperation(value = "志愿者签到")
    @PostMapping("/volunteerSignIn/{userId}/{activitiesId}/{address}")
    public ResResult<?> volunteerSignIn(@PathVariable Integer userId,@PathVariable Integer activitiesId,@PathVariable String address){
        if ((tblVolunteerMapper.selectOrderRecord(userId,activitiesId)) < 0){
            return new ResResult<>(201,"申请待定中或被拒绝");
        }
        Date date = new Date();
        Integer integer = tblVolunteerMapper.volunteerSignIn(userId ,activitiesId, address, date);
        if (integer < 1){
            return new ResResult<>(201,"您已签到");
        }
        return new ResResult<>(200,"签到成功");
    }

    @ApiOperation(value = "志愿者签退")
    @PostMapping("/volunteerSignOut/{userId}/{activitiesId}")
    public ResResult<?> volunteerSignOut(@PathVariable Integer userId, @PathVariable Integer activitiesId){
        Date date = new Date();
        Integer integer = tblVolunteerMapper.volunteerSignOut(userId,activitiesId, date);
        if (integer < 1){
            return new ResResult<>(201,"您已签退");
        }
        return new ResResult<>(200,"签退成功");
    }

    @ApiOperation(value = "获取某用户申请信息")
    @GetMapping("/getAllUserSignInfo/{userId}")
    public ResResult<?> getAllUserSignInfo(@PathVariable Integer userId){
        List<TblVolunteer> tblVolunteers = tblVolunteerMapper.getAllUserSignInfo(userId);
        return new ResResult<>(200,tblVolunteers);
    }

    @ApiOperation(value = "批量同意志愿者申请")
    @PutMapping("agreeVolunteerOrder/{volunteerIds}")
    public ResResult<?> agreeVolunteerOrder(@PathVariable Integer[] volunteerIds){
        Integer integer = tblVolunteerMapper.agreeVolunteerOrder(volunteerIds);
        if (integer < 1){
            return new ResResult<>(500,"操作失败");
        }
        return new ResResult<>(200,"操作成功");
    }

    @ApiOperation(value = "志愿者手动录入")
    @PostMapping("/volunteerWirthUserInfo/{idNumber}/{activitiesId}")
    public ResResult<?> volunteerWirthUserInfo(@PathVariable String idNumber,@PathVariable Integer activitiesId){
        return tblVolunteerService.volunteerWirthUserInfo(idNumber,activitiesId);
    }

}

